package com.ruoyi.pos.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.pos.domain.RejectItem;
import com.ruoyi.pos.mapper.RejectOrderMapper;
import com.ruoyi.pos.domain.RejectOrder;
import com.ruoyi.pos.service.IRejectOrderService;

/**
 * reject_orderService业务层处理
 * 
 * @author wzh
 * @date 2021-05-20
 */
@Service
public class RejectOrderServiceImpl implements IRejectOrderService 
{
    @Autowired
    private RejectOrderMapper rejectOrderMapper;

    /**
     * 查询reject_order
     * 
     * @param id reject_orderID
     * @return reject_order
     */
    @Override
    public RejectOrder selectRejectOrderById(Long id)
    {
        return rejectOrderMapper.selectRejectOrderById(id);
    }

    /**
     * 查询reject_order列表
     * 
     * @param rejectOrder reject_order
     * @return reject_order
     */
    @Override
    public List<RejectOrder> selectRejectOrderList(RejectOrder rejectOrder)
    {
        return rejectOrderMapper.selectRejectOrderList(rejectOrder);
    }

    /**
     * 新增reject_order
     * 
     * @param rejectOrder reject_order
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRejectOrder(RejectOrder rejectOrder)
    {
        int rows = rejectOrderMapper.insertRejectOrder(rejectOrder);
        insertRejectItem(rejectOrder);
        return rows;
    }

    /**
     * 修改reject_order
     * 
     * @param rejectOrder reject_order
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRejectOrder(RejectOrder rejectOrder)
    {
        rejectOrderMapper.deleteRejectItemById(rejectOrder.getId());
//        insertRejectItem(rejectOrder);
        return rejectOrderMapper.updateRejectOrder(rejectOrder);
    }

    /**
     * 批量删除reject_order
     * 
     * @param ids 需要删除的reject_orderID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRejectOrderByIds(Long[] ids)
    {
        rejectOrderMapper.deleteRejectItemByIds(ids);
        return rejectOrderMapper.deleteRejectOrderByIds(ids);
    }

    /**
     * 删除reject_order信息
     * 
     * @param id reject_orderID
     * @return 结果
     */
    @Override
    public int deleteRejectOrderById(Long id)
    {
        rejectOrderMapper.deleteRejectItemById(id);
        return rejectOrderMapper.deleteRejectOrderById(id);
    }

    /**
     * 新增退货信息
     * 
     * @param rejectOrder reject_order对象
     */
    public void insertRejectItem(RejectOrder rejectOrder)
    {
        List<RejectItem> rejectItemList = rejectOrder.getRejectItemList();
        Long id = rejectOrder.getId();
        if (StringUtils.isNotNull(rejectItemList))
        {
            List<RejectItem> list = new ArrayList<RejectItem>();
            for (RejectItem rejectItem : rejectItemList)
            {
                rejectItem.setId(id);
                list.add(rejectItem);
            }
            if (list.size() > 0)
            {
                rejectOrderMapper.batchRejectItem(list);
            }
        }
    }
}
